package top.xia17.plugins.fast.common.model;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * key value list
 * @author xia17
 * @since 2021/6/22 18:34
 */
public class KeyValueList<K,V> {

    private final List<KeyValue<K,V>> list = new ArrayList<>();

    public List<KeyValue<K,V>> getList(){
        return list;
    }

    public KeyValueList<K,V> add(KeyValue<K,V> data){
        this.list.add(data);
        return this;
    }

    public boolean hasKey(K key){
        return this.list.stream()
                .anyMatch(e->e.equalsKey(key));
    }

    public List<KeyValue<K,V>> find(K key){
        return this.list.stream().filter(e->e.equalsKey(key))
                .collect(Collectors.toList());
    }

    public KeyValue<K,V> findAny(K key){
        return this.list.stream().filter(e->e.equalsKey(key))
                .findAny().orElse(null);
    }




}
